//
//  BYJSearchViewController.m
//  BLDMusics
//
//  Created by 白云杰 on 16/3/24.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "BYJSearchViewController.h"

@interface BYJSearchViewController ()<INSSearchBarDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UINavigationControllerDelegate>

/** < 搜索框*/
@property (nonatomic, retain) INSSearchBar *search;
/** < 点击搜索时弹出页面的数据*/
@property (nonatomic, retain) NSMutableArray *searchArr;
/** collectionView */
@property (nonatomic, retain) UICollectionView *searchCollectionView;

@end

@implementation BYJSearchViewController

- (void)viewWillAppear:(BOOL)animated {
    //self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self dataHandle];
    [self initSearchController];
    [self createCollectionView];
    
}

#pragma mark - 搜索栏的创建
- (void)initSearchController {
    
    self.search = [[INSSearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH -100, 0)];
    _search.delegate = self;
    _search.searchField.placeholder = @"搜索单曲、歌手、专辑、歌单、mv";
    self.navigationItem.titleView = _search;
    
}

#pragma mark 点击键盘的搜索进行搜索
- (void)searchBarDidTapReturn:(INSSearchBar *)searchBar {

    BYJFinishSearchViewController *VC = [[BYJFinishSearchViewController alloc] init];
    VC.str = searchBar.searchField.text;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 收起键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 取消第一响应者
    [textField resignFirstResponder];
    // 回收键盘
    [textField endEditing:YES];
    
    return YES;
}

#pragma mark 点击后将要开始变化的时候
- (void)searchBar:(INSSearchBar *)searchBar willStartTransitioningToState:(INSSearchBarState)destinationState {
    
}

#pragma mark 搜索按钮停止时触发
- (void)searchBar:(INSSearchBar *)searchBar didEndTransitioningFromState:(INSSearchBarState)previousState {
}

#pragma mark search中搜索内容变化时
- (void)searchBarTextDidChange:(INSSearchBar *)searchBar {
}

#pragma mark - 创建collectionview
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 50) / 4, 30);
    
    self.searchCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _searchCollectionView.backgroundColor = [UIColor whiteColor];
    _searchCollectionView.delegate = self;
    _searchCollectionView.dataSource = self;
    [self.view addSubview:_searchCollectionView];
    
    [_searchCollectionView registerClass:[BYJSearchCollectionViewCell class] forCellWithReuseIdentifier:@"BYJSearchCollectionViewCell"];
}

#pragma nark 返回cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _searchArr.count;
}

#pragma mark 创建cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BYJSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BYJSearchCollectionViewCell" forIndexPath:indexPath];
    
    BYJSearchModel *model = _searchArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

#pragma mark cell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BYJFinishSearchViewController *VC = [[BYJFinishSearchViewController alloc] init];
    BYJSearchModel *model = _searchArr[indexPath.row];
    VC.str = model.val;
    
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark 数据解析
- (void)dataHandle {
    
    NSString *str = @"http://search.dongting.com/sug/billboard?";
    [APPTools GETWithURL:str par:nil success:^(id responseObject) {
        
        self.searchArr = [BYJSearchModel modelHandleWithArray:responseObject[@"data"]];
        [_searchCollectionView reloadData];
        
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark - UINavigationController的代理方法
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    
    if (operation == UINavigationControllerOperationPush) {
        PingTransition *ping = [PingTransition new];
        return ping;
    } else {
        return nil;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
